package com.lsk.mytuan.entity;

import com.lsk.mytuan.util.Storageable;
import com.lsk.mytuan.util.Storager;

import static com.lsk.mytuan.util.FloatCalc.*;

public class Store extends Storageable {
    private int id;
    private String name;
    private int userId;
    private Storager items;
    private float money;

    private static int idCounter = 0;
    private static Storager stores = new Storager();

    public Store(User user) {
        this.id = ++idCounter;
        this.name = user.getUsername();
        this.userId = user.getId();
        this.items = new Storager();
        this.money = 0F;
    }

    @Override
    public int getId () {
        return this.id;
    }

    @Override
    public String toString () {
        String result = "";
        result += "商家名称：  " + this.name + "\r\n";
        result += "商家用户ID：" + this.userId + "\r\n";
        result += "商家收入：  " + this.money + "\r\n";
        return result;
    }

    public static Store get (int id) {
        return (Store) stores.get(id);
    }

    public static Store findStoreByUserId (int userId) {
        for (int i = 1; i <= stores.length(); i ++) {
            Store store = (Store) stores.get(i);
            if (store != null && store.getUserId() == userId) {
                return store;
            }
        }
        return null;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void addMoney (float money) {
        this.money = fadd(this.money, money);
    }

    public void addItem (Item item) {
        this.items.put(item);
    }

    public Storager getItems () {
	return this.items;
    }

    public int getUserId () {
        return this.userId;
    }

    public void add () {
        stores.put(this);
    }
}

